//
//  hDisplayView.m
//  练习
//
//  Created by Hero11223 on 16/5/19.
//  Copyright © 2016年 zyy. All rights reserved.
//

#define MainScreen_width  [UIScreen mainScreen].bounds.size.width//宽
#define MainScreen_height [UIScreen mainScreen].bounds.size.height//高

#import "hDisplayView.h"

@interface hDisplayView ()<UIScrollViewDelegate>
{
    UIScrollView    *_bigScrollView;
    NSMutableArray  *_imageArray;
    UIPageControl   *_pageControl;
    
}
@property(nonatomic,strong)UIImageView * imageView;
@property(nonatomic,strong)NSTimer * timer;
@property(nonatomic,assign)NSInteger  time;


@end

@implementation hDisplayView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _imageArray = [NSMutableArray array];
                
        UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height)];
        //设置反野效果，不允许反弹，不显示水平滑动条，设置代理为自己
        scrollView.pagingEnabled = YES;//设置分页
        scrollView.bounces = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        _bigScrollView = scrollView;
        
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height)];
        _imageView.image = [UIImage imageNamed:@"启动页"];
        [_bigScrollView addSubview:_imageView];
        [self getWelcomeImage];
        
    }
    
    return self;
}




-(void)handleSingleTapFrom
{
    if (_pageControl.currentPage == _imageArray.count-1) {
        self.hidden = YES;
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _bigScrollView) {
        
        CGPoint offSet = scrollView.contentOffset;
        
        _pageControl.currentPage = offSet.x/(self.bounds.size.width);//计算当前的页码
        [scrollView setContentOffset:CGPointMake(self.bounds.size.width * (_pageControl.currentPage), scrollView.contentOffset.y) animated:YES];
        
    }
    
    if (scrollView.contentOffset.x == (_imageArray.count) *MainScreen_width) {
        self.hidden = YES;
    }
    
}


#pragma mark  获取首页启动页--=========================================
-(void)getWelcomeImage{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:@"4" forKey:@"Id"];
    self.shareApi = [LQRequestApi sharedInstance];
    self.shareApi.delegate=self;
//    [self.shareApi  requestOperationWithDic:dic withPath:F_queryAdvertismentById withHttpType:@"POST" withTag:JY_JY_queryAdvertisment];
    
    _timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
    [self startTimer];
}

#pragma mark -- Timer

- (void)changeTime{
    
    _time ++;
    if (_time==3) {
        self.hidden = YES;
        [self stopTimer];
    }
}

- (void)startTimer{
    _time = 0;
    [_timer fire];
}

- (void)stopTimer{
    [_timer invalidate];
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
//    
//    if (tag == JY_JY_queryAdvertisment){
//        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
//            
//            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
//            
//        }else  {
//            NSArray * array = database[@"result"];
//            if (array.count==0) {
//                return;
//            }
//            
//            _imageView.hidden = YES;
//            
//            NSString * images = database[@"result"][0][@"titleimg"];
//            NSArray * imageArray = [images componentsSeparatedByString:@","];
//            _imageArray = [imageArray mutableCopy];
//            
//            
//            for (int i = 0; i < _imageArray.count; i++) {
//                UIImageView *imageView = [[UIImageView alloc]init];
//                imageView.frame = CGRectMake(i * MainScreen_width, 0, MainScreen_width, MainScreen_height);
//                NSString *image = [NSString stringWithFormat:@"%@",_imageArray[i]];
//                [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]];
//                
//                [_bigScrollView addSubview:imageView];
//            }
//            
//            _bigScrollView.contentSize = CGSizeMake((_imageArray.count + 1)*MainScreen_width, MainScreen_height);
//            
//            UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(MainScreen_width/2, MainScreen_height - 60, 0, 40)];
//            pageControl.numberOfPages = _imageArray.count;
//            pageControl.backgroundColor = [UIColor clearColor];
//            [self addSubview:pageControl];
//            
//            _pageControl = pageControl;
//            
//            //添加手势
//            UITapGestureRecognizer *singleRecognizer;
//            singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapFrom)];
//            singleRecognizer.numberOfTapsRequired = 1;
//            [_bigScrollView addGestureRecognizer:singleRecognizer];
//            
//           }
//     }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
        self.hidden = YES;
    
}



@end
